package com.distribute.framework.core.sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"unchecked","unused","static-access", "serial"})
public class Table implements java.io.Serializable{
	private static final Long serialVersionUID = -1L;
	
	String name;
	String title;
	
	List<Column> columns=new ArrayList();

	public Table(String name)
	{
		this.name=name;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public List<Column> getColumns()
	{
		return columns;
	}

	public void setColumns(List<Column> columns)
	{
		this.columns = columns;
	}
	
	public void addColumns(Column column)
	{
		this.columns.add(column);
	}
	
	public Column getPrimaryKey()
	{
		for(Column c:this.columns)
		{
			if(c.getPrimary()==true)
				return c;
		}
		return null;
	}
	
	public String getPrimaryName()
	{
		Column primaryKey = this.getPrimaryKey();
		return primaryKey==null?null:primaryKey.getName();
	}
	
	
	public Map<String, Integer> getIndexes(){
		Map<String, Integer> indexes = new HashMap();
		for(Column c:this.columns){
			if(c.key || c.unique || c.primary){
				indexes.put(c.getName(), c.key?1:2);
			}
		}
		return indexes;
	}
	
	public Column getColumn(String name)
	{
		for(Column c:this.columns){
			if(c.getName().equals(name))
				return c;
		}
		return null;
	}

	public void setPrimaryKey(String pkColumnName)
	{
		for(Column c:this.columns){
			if(c.getName().equals(pkColumnName))
				c.setPrimary(true);
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}	
	
}